﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.Supervise.IBLL;
using HZ.Supervise.Model;
using System.Data;


public partial class EarlyWarning_WarningSetuptime : BasePage
{
    public EarlyWarning_WarningSetuptime()
    { }

    protected void Page_Load(object sender, EventArgs e)
    {
        this.btnEnter.Attributes.Add("onclick", "return CheckInput()");
        this.dropCity.Attributes.Add("onchange", "seleChangeHospital()");
        this.dropType.Attributes.Add("onchange", "seleChangDrugs()");
        if (!this.IsPostBack)
        {
            BindArea();
            BindSort();
            DataBind();
        }
        txtDrugsName.Attributes.Add("readonly", "readonly");
        txtHospital.Attributes.Add("readonly", "readonly");
        txtCompany.Attributes.Add("readonly", "readonly");
    }

    private void BindArea()
    {
        IList<Area> lstArea = new List<Area>();
        IBLL_Area ia = new HZ.Supervise.BLL.BLL_Area();
        int i;
        lstArea = ia.GetSonAreas(CurrentAccount.GetUser().Org.Area.AreaID,1,0,out i);
        foreach (Area area in lstArea)
        {
            ListItem ddlitem = new ListItem();
            ddlitem.Text = area.AreaName;
            ddlitem.Value = area.AreaID;
            dropCity.Items.Add(ddlitem);
        }

        

    }

    private void BindSort()
    {
        HZ.Supervise.IBLL.IExecuteContext ie = HZ.Supervise.BLLFactory.ExecuteContext();
        ie.QueryObject = new HZ.Supervise.Model.QueryObject()
        {
            GroupField = GroupByField.SortID,
            SortField = "SortID"
        };
        System.Data.DataTable dt = ie.Execute();

        this.dropType.DataTextField = "SortName";
        this.dropType.DataValueField = "SortId";
        this.dropType.DataSource = dt;
        this.dropType.DataBind();

        ListItem item = new ListItem();
        item.Text = "全部";
        item.Value = "";
        dropType.Items.Insert(0, item);
    }

    private void DataBind()
    {
        int t = 0;
        HZ.Supervise.IBLL.IBLL_PreWarning pre = HZ.Supervise.BLLFactory.IBLL_PreWarning();
        System.Data.DataTable dt = pre.GetTradePreWarning(null, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out t);
        if (dt.Rows.Count > 0)
        {
            foreach (DataRow row in dt.Rows)
            {
                if (row["HospitalID"].ToString() != "所有医院" && row["HospitalName"] == DBNull.Value)
                {
                    row["Hospitalname"] = "多家医院";
                }
                if (row["CompanyId_PS"].ToString() != "所有企业" && row["CompanyName"] == DBNull.Value)
                {
                    row["CompanyName"] = "多家企业";
                }
                if (row["WarningType"] != DBNull.Value)
                {
                    if (row["WarningType"].ToString() == "2")
                    {
                        row["CompanyName"] = "无需企业";
                    }
                }
            }
        }

        AspNetPager1.RecordCount = t;
        this.gridInfo.DataSource = dt;
        this.gridInfo.DataBind();
    }

    protected void btnEnter_Click(object sender, EventArgs e)
    {
        string _AreaId = this.dropCity.SelectedItem.Value; //区域
        string _DrugType = this.dropType.SelectedItem.Value == "" ? "所有类别" : this.dropType.SelectedItem.Value; //药品类别
        string _DrugName = HZ.Fun.CheckString(this.txtDrugsName.Text.Trim()); //药品名称
        string _DrugId = HZ.Fun.CheckString(this.hddGoodsId.Value); //药品名称（编号）
        string _HospitalName = HZ.Fun.CheckString(this.txtHospital.Text.Trim());//医院名称
        string _HospitalId = HZ.Fun.CheckString(this.hidHospitalId.Value);//医院编号
        string _ComapnyName = HZ.Fun.CheckString(this.txtCompany.Text.Trim());//企业名称
        string _CompanyId = HZ.Fun.CheckString(this.hidCompanyId.Value);//企业编号

        string _ResponseType = this.dropListType.SelectedItem.Value;  //响应类型
        string _ResponseTime = HZ.Fun.CheckString(this.txtTime.Text);//响应时间
        string _Responsedays = this.dropTime.SelectedItem.Text;  //
        string _ResponseLevel = this.dropJiebie.SelectedItem.Value; //响应级别

        
        if (!HZ.Fun.IsInt(_ResponseTime))
        {
            ClientScriptManager a = Page.ClientScript;
            a.RegisterClientScriptBlock(this.GetType(), "MyScript", "javascript:alert('响应时间有误!')", true);
            return;
        }

        if (string.IsNullOrEmpty(_DrugName)) { _DrugId = "所有药品"; }
        if (string.IsNullOrEmpty(_HospitalName)) { _HospitalId = "所有医院"; }
        if (string.IsNullOrEmpty(_ComapnyName)) { _CompanyId = "所有企业"; }

        HZ.Supervise.Model.TradePreWarning Trade = new TradePreWarning();
        HZ.Supervise.IBLL.IBLL_PreWarning pre = HZ.Supervise.BLLFactory.IBLL_PreWarning();

        Trade.AreaID = _AreaId;   //地区编号
        Trade.SortID = _DrugType; //类别编号
        Trade.ProcureCatalogID = _DrugId; //药品编号

        Trade.HospitalID = _HospitalId;//医院编号
        Trade.CompanyID_PS = _CompanyId;//企业编号

        Trade.WarningType = int.Parse(_ResponseType); //响应类型

        if (pre.TranCheckDataSource(Trade))
        {
            HZ.Web.WebUtil.Alert("已设置相同的预警！");
            return;
        }

        int _time = 0;
        switch (_Responsedays)
        {
            case "天":
                _time = int.Parse(_ResponseTime);
                break;
            case "周":
                _time = int.Parse(_ResponseTime) * 7;
                break;
            case "月":
                _time = int.Parse(_ResponseTime) * 30;
                break;
        }

        Trade.WarningDate = _time;
        Trade.WarningLevel = int.Parse(_ResponseLevel);

        if (pre.AddTradePreWarning(Trade)>0)
        {
            HZ.Web.WebUtil.Alert("制定成功！");
            HZ.Web.WebUtil.RedirectByScript("WarningSetuptime.aspx");
        }
    }
    protected void gridInfo_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label LabType = (Label)e.Row.FindControl("labType");
            Label LabLeve = (Label)e.Row.FindControl("labLeve");

            switch(LabType.Text)
            {
                case "0":
                    LabType.Text = "医院已提交采购单企业未确认";
                    break;
                case "1":
                    LabType.Text = "企业已确认定单医院未入库";
                    break;
                case "2":
                    LabType.Text = "企业已开发票中心未审核";
                    break;
                case "3":
                    LabType.Text = "医院已提交退货单企业未确认";
                    break;
                case "4":
                    LabType.Text = "医院已提交回款单企业未确认";
                    break;
                case "5":
                    LabType.Text = "医院已入库未回款";
                    break;
            }

            switch(LabLeve.Text)
            {
                case "1":
                    LabLeve.Text = "一级预警";
                    break;
                case "2":
                    LabLeve.Text = "二级预警";
                    break;
                case "3":
                    LabLeve.Text = "三级预警";
                    break;
            }

            string _AreaID = ((DataRowView)e.Row.DataItem).Row.ItemArray[1].ToString();
            string _HospitalID = ((DataRowView)e.Row.DataItem).Row.ItemArray[4].ToString();
            string _CompanyId = ((DataRowView)e.Row.DataItem).Row.ItemArray[5].ToString();

            e.Row.Cells[2].Attributes.Add("onclick", "javascript:window.open('showHospital.aspx?HospitalID=" + Server.UrlEncode(_HospitalID) + "&&AreaID=" + Server.UrlEncode(_AreaID) + "','showHospital1','width=600,height=400,resizable=0,scrollbars=0, status=0, toolbar=0, menubar=0')");

            if (e.Row.Cells[3].Text != "无需企业")
            {
                e.Row.Cells[3].Attributes.Add("onclick", "javascript:window.open('showCompany.aspx?CompanyID=" + Server.UrlEncode(_CompanyId) + "','showCompany','width=600,height=400,resizable=0,scrollbars=0, status=0, toolbar=0, menubar=0')");
            }

            e.Row.Cells[2].Attributes.Add("onmouseover", "this.style.color='red'");
            e.Row.Cells[2].Attributes.Add("onmouseout", "this.style.color=''");

            e.Row.Cells[3].Attributes.Add("onmouseover", "this.style.color='red'");
            e.Row.Cells[3].Attributes.Add("onmouseout", "this.style.color=''");
            
        }
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        DataBind();
    }
}
